package com.rc.rc_exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rc.rc_exam.entity.PageResult;
import com.rc.rc_exam.entity.RCExam;
import com.rc.rc_exam.entity.RCExamTest;
import com.rc.rc_exam.entity.web.Result;
import com.rc.rc_exam.service.RCExamService;
import com.rc.rc_exam.service.RCExamTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
@CrossOrigin
@Api(tags = "试题管理接口")
public class RcExamController {

    @Autowired
    private RCExamService rcExamService;

    @Autowired
    private RCExamTestService rcExamTestService;

    /**
     * 上传试题excel
     * @param file
     * @return
     */
    @PostMapping("uploadExamExcel")
    @ApiOperation("上传试题excel")
    public Result uploadExamExcel(@RequestParam("file") MultipartFile file){
        String result = rcExamService.createRcExamDataByExcel(file);
        return Result.ok(result);
    }

    /**
     * 查询试题列表
     * @return
     */
    @GetMapping("/findQuestions")
    @ApiOperation("查询试题列表")
    public Result selectRCExams(){
        List<RCExam> list = rcExamService.list();
        return Result.ok(list);
    }

    /**
     * 随机查询考试题目
     * @return
     */
    @GetMapping("/findRandomQuestions")
    @ApiOperation("随机查询试题列表")
    public Result selectRCExamsRandom(@RequestParam int num){
        System.out.println(num);
        List<RCExam> rcExamList = rcExamService.findExamByPage(num);
        return Result.ok(rcExamList);
    }

    /**
     *
     * @param currentNum 当前数量
     * @param num 考试题目数量
     * @return
     */
    @GetMapping("/findSequenceQuestions")
    @ApiOperation("顺序查询试题列表")
    public Result selectRCExamsSequence(@RequestParam int currentNum,@RequestParam int num){
        System.out.println(num);
        List<RCExam> rcExamList = rcExamService.findSequenceExamByPage(currentNum,num);
        QueryWrapper<RCExamTest> testQueryWrapper = new QueryWrapper<>();
        testQueryWrapper.like("test_name","顺序考试");
        List<RCExamTest> rcExamTestList = rcExamTestService.list(testQueryWrapper);
        List<String> lastNumList = rcExamTestList.stream().sorted(Comparator.comparing(RCExamTest::getOrderNum)).map(RCExamTest::getLastNum).collect(Collectors.toList());
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("rcExamList",rcExamList);
        resultMap.put("lastNum",lastNumList.isEmpty()?0:lastNumList.get(0));
        return Result.ok(resultMap);
    }

    /**
     * 保存考试记录
     * @return
     */
    @PostMapping("/saveTest")
    @ApiOperation("保存考试记录")
    public Result saveTest(@RequestParam Map<String,Object> paramMap){
        String result = rcExamService.saveTestRecord(paramMap);
        return Result.ok(result);
    }

    /**
     * 分页查询题库数据
     * @return
     */
    @PostMapping("/findExamByPage")
    @ApiOperation("查询库存数量")
    public Result findExamByPage(@RequestParam Map<String,Object> paramMap){
        PageResult<RCExam> result = rcExamService.findExamsByPage(paramMap);
        return Result.ok(result);
    }
}
